package com.heeexy.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.heeexy.example.dao.ProjectDao;
import com.heeexy.example.service.ProjectService;
import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.util.CommonUtil;
import com.heeexy.example.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class ProjectServicelmpl implements ProjectService{

    @Autowired
    private ProjectDao projectDao;

    /**
     * 新增项目
     */
    @Override
    public JSONObject addProject(JSONObject jsonObject) {
        int exist = projectDao.queryExistProjectId(jsonObject);
        if (exist > 0) {
            return CommonUtil.errorJson(ErrorEnum.E_10009);
        }
        projectDao.addProject(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     * 所有项目列表
     */
    @Override
    public JSONObject listProjectAll(JSONObject jsonObject){
        CommonUtil.fillPageParam(jsonObject);
        int count = projectDao.countProject(jsonObject);
        List<JSONObject> list = projectDao.listProjectAll(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 审批中项目列表
     */
    @Override
    public JSONObject listProject(JSONObject jsonObject){
        CommonUtil.fillPageParam(jsonObject);
        int count = projectDao.countProject(jsonObject);
        List<JSONObject> list = projectDao.listProject(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 更新项目
     */
    @Override
    public JSONObject updateProject(JSONObject jsonObject) {
        projectDao.updateProject(jsonObject);
        return CommonUtil.successJson();
    }
    /**
     * 删除项目
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject deleteProject(JSONObject jsonObject){
        projectDao.deleteProject(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     *用户提交项目
     */
    @Override
    public JSONObject submitProject(JSONObject jsonObject){
        projectDao.submitProject(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     *当前用户项目
     */
    @Override
    public JSONObject preUserProjectList(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = projectDao.preUserProjectNumber(jsonObject);
        List<JSONObject> list = projectDao.preUserProjects(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 更新项目状态
     */
    @Override
    public JSONObject updateProjectState(JSONObject jsonObject) {
        projectDao.updateProjectState(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     * 用户更新申请项目信息
     */
    @Override
    public  JSONObject updateApplyProject(JSONObject jsonObject) {
        projectDao.updateApplyProject(jsonObject);
        return CommonUtil.successJson();
    }
    /**
     * 用户完成项目
     */
    @Override
    public JSONObject updateProjectComplete(JSONObject jsonObject) {
        projectDao.updateProjectComplete(jsonObject);
        return CommonUtil.successJson();
    }
}
